//
//  MNDTenderInfoViewController.m
//  minidai
//
//  Created by 谭谭伟 on 17/5/21.
//  Copyright © 2017年 迷你金服. All rights reserved.
//

#import "MNDTenderInfoViewController.h"
#import "MNDTenderInfoCell.h"
#import "MNDCreditorRecordModel.h"

@interface MNDTenderInfoViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSMutableArray *creditorsArrayM;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) UIView *maskView;
@property (weak, nonatomic) IBOutlet UILabel *topTipLabel;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomMarginConstraint;

@end

@implementation MNDTenderInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.estimatedRowHeight = 40;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
        // Do any additional setup after loading the view.
//    [self creatMaskView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (kDevice_iPad) {
        self.bottomMarginConstraint.constant = 114;
    }else {
        if (kScreenWidth > 375) {
            self.bottomMarginConstraint.constant = 114;
        }else {
            self.bottomMarginConstraint.constant = 50;
        }

    }
}


- (NSMutableArray *)creditorsArrayM {
    if (!_creditorsArrayM) {
        _creditorsArrayM = [NSMutableArray array];
    }
    return _creditorsArrayM;
}

- (void)setCreditor:(NSArray *)creditor {
    _creditor = creditor;
    //债权记录
//    NSArray *creditorsArray = _creditor.allValues;
    for (NSDictionary *dict in creditor) {
        MNDCreditorRecordModel *creditorRecordModel = [MNDCreditorRecordModel yy_modelWithDictionary:dict];
        [self.creditorsArrayM addObject:creditorRecordModel];
    }
    if (self.creditorsArrayM.count > 0) {
//        [self creatMaskView];
//        self.maskView.hidden = YES;
    }else {
        [self creatMaskView];
//        self.maskView.hidden = NO;
    }
    [self.tableView reloadData];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.creditorsArrayM.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MNDCreditorRecordModel *creditorRecordModel = self.creditorsArrayM[indexPath.row];
    MNDTenderInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MNDTenderInfoCell"];
    cell.numberLabel.text = creditorRecordModel.key;
    cell.userNameLabel.text = creditorRecordModel.user_name;
    cell.creditorPrincipalLabel.text = creditorRecordModel.return_principal;
    return cell;
}

- (void)creatMaskView {
    UIView *maskView = [[UIView alloc]initWithFrame:self.view.bounds];
    maskView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1.0];
//    [self.view insertSubview:maskView aboveSubview:self.tableView];
    [self.tableView addSubview:maskView];
    UIImageView *imgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"financen_detail_data_empty"]];
    [maskView addSubview:imgV];
    imgV.layer.cornerRadius = imgV.bounds.size.height*0.5;
    imgV.clipsToBounds = YES;
    [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(maskView);
        make.centerY.equalTo(maskView).mas_offset(-80);
    }];
    self.maskView = maskView;

}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (kDevice_iPad) {
        return;
    }
    CGFloat distance = scrollView.contentOffset.y;
    if (distance < -10) {
        NSLog(@"------%f------",distance);
        self.topTipLabel.alpha = (-distance-10)/30;
        NSLog(@"------透明度%f------",self.topTipLabel.alpha);
    }
    if (distance == 0) {
        self.topTipLabel.alpha = 0;
    }
    if (distance < -40) {
        self.topTipLabel.text = @"释放回到出借详情";
    }else {
        self.topTipLabel.text = @"下拉回到出借详情";
    }
}

//- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
//    if (self.delegate && [self.delegate respondsToSelector:@selector(tenderInfoRecordBottomViewDidEndDragging:)]) {
//        
//        [self.delegate tenderInfoRecordBottomViewDidEndDragging:self.tableView];
//    }
//}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    if (self.delegate && [self.delegate respondsToSelector:@selector(tenderInfoRecordBottomViewDidEndDragging:)]) {
        
        [self.delegate tenderInfoRecordBottomViewDidEndDragging:self.tableView];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
